package com.code.day22;

import org.junit.Test;

import javax.swing.plaf.PanelUI;
import java.beans.Transient;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ConstructorDemo {
    @Test
    public void method01(){
        Class<Person> personClass = Person.class;
        Constructor<?>[] constructors = personClass.getConstructors();
        for (Constructor<?> constructor : constructors) {
            System.out.println(constructor);
        }
    }
    @Test
    public void method02() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Class<Person> personClass = Person.class;
        Constructor<Person> constructor = personClass.getConstructor();
        System.out.println(constructor);
        Person person = constructor.newInstance();
        System.out.println( person);
    }

    @Test
    public void method03() throws InstantiationException, IllegalAccessException {
        Class<Person> personClass = Person.class;
        Person person = personClass.newInstance();
        System.out.println(person);
    }
    @Test
    public  void method04() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Class<Person> personClass = Person.class;
        Constructor<Person> constructor = personClass.getConstructor(String.class, int.class);
        Person person = constructor.newInstance("刘大胆",18);
        System.out.println(person);
    }

    @Test
    public void method05(){
        Class<Person> personClass = Person.class;
        Constructor<?>[] declaredConstructors = personClass.getDeclaredConstructors();
        for (Constructor<?> declaredConstructor : declaredConstructors) {
            System.out.println(declaredConstructor);
        }
    }

    @Test
    public void method06() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Class<Person> personClass = Person.class;
        Constructor<Person> declaredConstructor = personClass.getDeclaredConstructor(String.class);
        //解除私有权限
        declaredConstructor.setAccessible(true);
        Person person = declaredConstructor.newInstance("刘大胆");
        System.out.println(person);
    }
    @Test
    public void method07() throws InstantiationException, IllegalAccessException {
        Class<Person> personClass = Person.class;
        Method[] methods = personClass.getMethods();
        for (Method method : methods) {
            System.out.println(method);
        }
    }
    @Test
    public  void method08() throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Class<Person> personClass = Person.class;
        Person person = personClass.newInstance();
        Method setName = personClass.getMethod("setName", String.class);
        setName.invoke(person,"刘大胆");

        Method getName = personClass.getMethod("getName");
        Object invoke = getName.invoke(person);
        System.out.println(invoke);

    }
    @Test
    public  void method09() throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        Class<Person> personClass = Person.class;
        Person person = personClass.newInstance();
        Method declaredMethod = personClass.getDeclaredMethod("show");
        declaredMethod.setAccessible(true);
        declaredMethod.invoke(person);
    }

}
